package www.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import www.admin.dao.IGroupResourceDao;
import www.admin.model.GroupResource;
import www.admin.mybatis.Criteria;
import www.admin.service.IGroupResourceService;
import www.admin.utils.MessageConstant;

@Service
public class GroupResourceServiceImpl implements IGroupResourceService{

	public Map<String, List<GroupResource>>  queryPage (Criteria<GroupResource> param){
		List<GroupResource> list = groupResourceRepository.queryPage(param);
		list.forEach(f -> {
			Criteria<GroupResource> params = new Criteria<GroupResource>();
			params.addParam("parent_resource_id", f.getResourceId());
			params.addParam("resourceIdList", param.getList());
			List<GroupResource> list2 = groupResourceRepository.queryPage(params);
			f.setChildren(list2);
		});
		Map<String, List<GroupResource>> map = convertListBean(param.getTitie(),list);
	    return map; 
	}
	
	public Integer queryPageCount (Criteria<GroupResource> param){
		return (Integer) groupResourceRepository.queryPageCount(param);
	}

	public GroupResource  queryEntity (String name){
		return groupResourceRepository.queryEntity(name);
	}

	public String deleteById (Integer id ){
		int result = groupResourceRepository.deleteById(id);
			if ( result != 1) {
					return MessageConstant.USER_TYPE_3;
			}
			return null;
	}

	public String insert(GroupResource record ){
		int result = groupResourceRepository.insert(record);
			if ( result != 1) { 
					return MessageConstant.USER_TYPE_4;
			}
			return null;
	}

	public String update (Criteria<GroupResource> param){
		int result = groupResourceRepository.update(param);
			if ( result != 1) { 
					return MessageConstant.USER_TYPE_5;
			}
			return null;
	}
	

	public List<GroupResource> queryPageList(Criteria<GroupResource> param) {
		return groupResourceRepository.queryPage(param);
	}

	public static <T> Map<String, List<T>> convertListBean(String titie, List<T> beanList) {
		Map<String, List<T>> maps = new HashMap<String, List<T>>();
		maps.put(titie, beanList);
	    return maps;
	}
	
	@Resource
	private IGroupResourceDao groupResourceRepository;

}